package com.fifth.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.commodity.vo.SortVo;
import com.fifth.commodity.vo.StateVo;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Sort;
import com.fifth.commodity.mapper.SortMapper;
import com.fifth.commodity.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SortServiceImpl implements SortService {
    @Autowired
    private SortMapper sortMapper;
    /*
    * 删除
    * */
    @Override
    public CommonResult SortDelete(String id) {
        int i = sortMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }
    /*
    * 查询
    * */
    @Override
    public CommonResult SortSelectAll(Sort sort) {

        List<Sort> sort1 = sortMapper.selectAll(sort);
        return new CommonResult(2000,"查询所有",sort1);
    }
    /*
    * 添加
    * */
    @Override
    public CommonResult SortInsert(Sort sort) {
        int i = sortMapper.deleteById(sort);
        return new CommonResult(2000,"添加成功",i);
    }
    /*
    * 修改
    * */
    @Override
    public CommonResult SortUpdate(Sort sort) {
        SortMapper sortMapp = this.sortMapper;
        return new CommonResult(2000,"修改成功",sortMapp);
    }

    /**
     * 查询所有商品类别
     * @return
     */
    @Override
    public CommonResult findAllSort() {
        List<Sort> sorts = sortMapper.selectList(null);
        return new CommonResult(2000,"查询成功",sorts);
    }

    /**
     * 根据类别分页查询所有类别信息
     * @param pageCurrent
     * @param pageSize
     * @param sortVo
     * @return
     */
    @Override
    public CommonResult findSortByCondition(Integer pageCurrent, Integer pageSize, SortVo sortVo) {
        IPage<Sort> page = new Page<>(pageCurrent,pageSize);
        QueryWrapper<Sort> queryWrapper = new QueryWrapper<>();
        if (sortVo.getSortid()!=null){
            queryWrapper.eq("id",sortVo.getSortid());
        }
         if (sortVo.getStatus()!=null){
           queryWrapper.eq("status",sortVo.getStatus());
        }
        if (sortVo.getSortid()==null && sortVo.getStatus()==null){
            queryWrapper = null;
        }
        IPage<Sort> page1 = sortMapper.selectPage(page, queryWrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    /**
     * 根据id删除类别
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteSort(Integer id) {
        int i = sortMapper.deleteById(id);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(4000,"删除失败",null);
    }

    /**
     * 修改类别的审核状态
     * @param stateVo
     * @return
     */
    @Override
    public CommonResult updateStatus(StateVo stateVo) {
        Sort sort = new Sort();
        sort.setCheckContent(stateVo.getCheckContent());
        sort.setStatus(stateVo.getState());
        QueryWrapper<Sort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",stateVo.getId());
        int update = sortMapper.update(sort, queryWrapper);
        if (update==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    /**
     * 查询所有类别
     * @return
     */
    @Override
    public List<Sort> selectAllSort() {
        List<Sort> sorts = sortMapper.selectList(null);
        return sorts;
    }
}
